package RandomValue;

import java.io.*;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 2020-07-27
 * 模拟生成借据Vintage数据
 * 测试通过
 */
public class Vintage {

    public static String num = "0123456789";
    public static String[] product_code="JXJ,JXJDD,JXJXX,JXJBB,JXJYQS,DXJ,YMD".split(",");
    public static int[] status={1,2,3,4,5,6};
    public static String[] loan_age="1,2,3,4".split(",");
    public static String[] union_name="广州银行,柳州银行,渤海银行,昆仑银行,稠州银行,石嘴山银行,浙商银行,中融信托,光大银行,幸福消金,稠州银行,华能信托,东海银行,厦门银行,潍坊银行,江苏银行,五矿信托,平安银行,重庆银行,富民银行,工商银行,苏宁银行,烟台银行,达州银行,莱商银行,北京银行,宁波银行,云南信托,宁波银行,潍坊银行,三湘银行,华夏银行".split(",");

    public static void main(String[] args) throws IOException {

        List<VintageModule> list = new ArrayList<>();

        System.out.println("job is running");
        for (int i = 0; i < 600; i++) {
            String dt=getDate();
//            int customer_id=getUid(100000,999999);
            String customer_id=getNumber(8);
            String product_code=getProduct();
            String loan_id=getNumber(15);
            int loan_status=getStatus();
            String kpi=getKpi();
            int issue_amount=getNum(10000000,99999999);
            int principal_payable=getNum(10000,99999999);
            int overdue_principal_payable=getNum(0,99999999);
            int principal_repay=getNum(0,99999999);
            String unionloan_name=getUnionName();
            VintageModule vintageModule = new VintageModule(dt,customer_id,product_code,loan_id,loan_status,kpi,issue_amount,principal_payable,overdue_principal_payable,principal_repay,unionloan_name);
            list.add(vintageModule);

        }
        PointsToCsvFile(list);
        System.out.println("job is running over");
    }

    /**
     * 在指定区间内随机获取一个数字
     *
     * @param start 开始
     * @param end   结束
     */
    private static int getNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }

    /**
     * 递增生成切片时间
     */
    public static String getDate(){

        Format f = new SimpleDateFormat("yyyy-MM-dd");

        Date dt = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
//        for(int i=0;i<5;i++){
//            c.add(Calendar.DAY_OF_MONTH, i);// 今天+1天
//            i++;
//
//        }
//        Date tomorrow = c.getTime();
        return f.format(dt);

    }

    /**
     * 随机生成产品码
     */

    public static String getProduct(){
        int index = getNum(0, product_code.length - 1);
        String prod = product_code[index];
        return prod;
    }

    /**
     * 随机生成一个数字的字符串
     * customer_id,loan_id
     * @param length 长度
     */
    public static String getNumber(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = (int) (Math.random() * num.length());
            sb.append(num.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 随机生成loan_status
     */

    public static int getStatus(){
        int index = getNum(0, status.length - 1);
        int stat = status[index];
        return stat;
    }

    /**
     * 随机生成借据逾期期数kpi
     */

    public static String getKpi(){
        int index = getNum(0, loan_age.length - 1);
        String kpi = loan_age[index];
        return kpi;
    }

    /**
     * 随机生成助贷银行
     */

    public static String getUnionName(){
        int index = getNum(0, union_name.length - 1);
        String unionloan_name = union_name[index];
        return unionloan_name;
    }




    /**
     * 生成csv
     *
     * @param pointsList
     */
    public static void PointsToCsvFile(List<VintageModule> pointsList) {

        // 表格头
        String[] headArr = {"dt","customer_id","product_code","loan_id","loan_status","kpi","issue_amount","principal_payable","overdue_principal_payable","principal_repay","unionloan_name"};  // 设置表头
        //CSV文件路径及名称
        LocalDateTime localDateTime = LocalDateTime.now();
//        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        ///home/YY/test/out
        String filePath = "./src/data"; //CSV文件路径
        String fileName = "CSV_" + df.format(localDateTime) + ".csv";//CSV文件名称
        File csvFile = null;
        BufferedWriter csvWriter = null;
        try {
            csvFile = new File(filePath + File.separator + fileName);
            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            csvFile.createNewFile();
            // GB2312使正确读取分隔符","
            csvWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "utf-8"), 1024);

            // 写入文件头部标题行

            csvWriter.write(String.join(",", headArr));
            csvWriter.newLine();

            // 写入文件内容
            for (VintageModule points : pointsList) {
                csvWriter.write(points.toShow());
                csvWriter.newLine();
            }
            csvWriter.flush();
        } catch (Exception e) {
        } finally {
            try {
                csvWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}
